import Vue from 'vue'
import Vuex from 'vuex'
// vuex数据的持久化插件
import persistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: '',
    userId: '',
    collection: [],
    shoppingCart: [],
  },
  mutations: {
    // 用户名
    addUser(state, data) {
      state.user = data.userName
      state.userId = data.user_id
    },
    // 退出登录
    outUser(state) {
      state.user = ''
    },
    setCartData(state, data) {
      state.allNum = 0
      data.forEach(item => {
        item.money = item.num * item.price
        item.visible = false
        state.allNum += item.num
      })
      state.cartData = data
    },
    setOrder(state, val) {
      state.nOrder = val
    }
  },
  actions: {},
  modules: {},
  getters: {},
  plugins: [persistedState()]
})